/*
 * Copyright 1999,2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.webduff.methods;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.googlecode.webduff.WebdavStatus;
import com.googlecode.webduff.exceptions.AccessDeniedException;
import com.googlecode.webduff.exceptions.ForbiddenException;
import com.googlecode.webduff.exceptions.MethodResponseError;
import com.googlecode.webduff.exceptions.WebdavException;
import com.googlecode.webduff.io.URI;
import com.googlecode.webduff.locking.AcquiredLock;
import com.googlecode.webduff.locking.LockFailedException;
import com.googlecode.webduff.locking.ResourceLocks;

import java.io.IOException;

public class DoPut extends AbstractMethod {

	private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DoPut.class);

    public MethodResponse execute(HttpServletRequest req, HttpServletResponse resp) throws IOException, MethodResponseError {
    	URI theURI = getURIForRequest(req);
        MethodResponse theResponse = new MethodResponse(resp);
        AcquiredLock acquiredLock = null;
        
        try {
			acquiredLock = acquireExclusiveLock(theURI, ResourceLocks.INFINITE, req);
		} catch (LockFailedException e) {
			throw new MethodResponseError(WebdavStatus.SC_LOCKED);
		}
		
        URI parentURI = theURI.stripLast();
        
        try {
            if (parentURI != null) {
            	if(!store.isFolder(parentURI)) {
            		store.createFolder(parentURI);
            	}
            }
            if (!store.isFolder(theURI)) {
                if (!store.objectExists(theURI)) {
                	if(!store.canCreateResource(theURI)) {
                		throw new ForbiddenException();
                	}
                	
                    store.createResource(theURI);
                    log.debug("Created.");
                    theResponse.setStatusCode(WebdavStatus.SC_CREATED);
                } else {
                	log.debug("No Content.");
                	theResponse.setStatusCode(WebdavStatus.SC_NO_CONTENT);
                }
                store.setResourceContent(theURI, req.getInputStream(), theMimeTyper.getMimeType(theURI), req.getCharacterEncoding());
            }
        } catch (AccessDeniedException e) {
        	log.debug("DoPut", e);
            throw new MethodResponseError(WebdavStatus.SC_FORBIDDEN);
        } catch(ForbiddenException e) {
        	log.debug("DoPut", e);
        	throw new MethodResponseError(WebdavStatus.SC_CONFLICT);
        } catch (WebdavException e) {
        	log.error("DoPut", e);
        	throw new MethodResponseError(WebdavStatus.SC_INTERNAL_SERVER_ERROR);
        } finally {
        	acquiredLock.unlock();
        }
        
        return theResponse;
    }
    
}